<?php
defined('PG_PAGE') or die();
require_once PG_ROOT.'/include/phpmailer/class.phpmailer.php';

function send_email($recipient, $sender='', $subject, $message, $order_id=0){
	global $setting, $database;
  
  if (defined('PG_DEBUG')) return true;
  
  $mail       = new PHPMailer();
  $mail->IsSMTP(true);            // use SMTP

  $mail->SMTPDebug  = 1;        // enables SMTP debug information (for testing)
                                  // 1 = errors and messages
                                  // 2 = messages only
  /*$mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->SMTPSecure = "ssl";
  $mail->Host       = "email-smtp.us-east-1.amazonaws.com"; // Amazon SES server, note "tls://" protocol
  $mail->Port       = 465;                    // set the SMTP port
  $mail->Username   = "AKIAIZHO5BHVXNCZT4TQ";  // SES SMTP  username
  $mail->Password   = 'AjPjCwQ+gF77kw4yIar6UlC4h+y5MDJPWMpbDe5CFvt8';  // SES SMTP password

  $mail->SetFrom('noreply@onecard.vn', 'Onecard .ltd');*/
  
  $mail->SMTPAuth   = false;                  // enable SMTP authentication
  //$mail->SMTPSecure = "none";
  $mail->Host       = "email-smtp.giga.vn"; // Amazon SES server, note "tls://" protocol
  $mail->Port       = 587;                    // set the SMTP port
  $mail->Username   = "do-not-reply@onecard.vn";  // SES SMTP  username
  $mail->Password   = 'EV2yY9w4W8rUnDtj5yDi2fvCvsogDQbr';  // SES SMTP password

  $mail->SetFrom('noreply@onecard.vn', 'Onecard .ltd');
  $mail->AddReplyTo('noreply@onecard.vn', 'Onecard .ltd');
  $mail->Subject    = $subject;
  $mail->MsgHTML($message);
  $mail->AddAddress($recipient);
  $mail->AddBCC('dungdh@onecard.vn', 'Onecard Admin');
  
  $mail->charSet = "UTF-8";
  if (defined('PG_DEBUG') && PG_DEBUG){
    $result = true;
  }else
    $result = $mail->Send();

  if(!$result) {
    return false;
  } else {
    $database->db_query("UPDATE orders SET order_sendmail=1 WHERE order_id=$order_id");
    return true;
  }
} // END FUNCTION

function send_email_system($recipient, $sender='', $subject, $message, $order_id=0){
	global $setting, $database;
	
	// DECODE SUBJECT AND EMAIL FOR SENDING
	$subject = htmlspecialchars_decode($subject, ENT_QUOTES);
	$message = htmlspecialchars_decode($message, ENT_QUOTES);

	// ENCODE SUBJECT FOR UTF8
	$subject="=?UTF-8?B?".base64_encode($subject)."?=";

	// REPLACE CARRIAGE RETURNS WITH BREAKS
	$message = str_replace("\n", "", $message);
	
	$message = '
	<div style="width: 723px; background-color: #FFFFFF; border: 1px solid #CDCDCD; position: relative; padding: 10px; font-family: Arial, Tahoma, Verdana; font-size: 12px; color: #999; display: inline-block; border-radius:5px; -webkit-border-radius:5px; -moz-border-radius:5px;">
	    <div style="width: 100%; height: 84px; border-bottom: 1px dotted #999999;"> <a href="'.PG_URL_ABSOLUTE.'" style="width: 200px; height: 53px; display: block; float: left;"><img src="'.PG_URL_ABSOLUTE.'templates/images/logo.png" alt="Logo Onecard" width="200" height="53" border="0" /></a>
	        <div style="float: right; font-size: 12px; color: #999; text-align:right">
	            <h2 style="color: #FE890A; font-size: 22px; margin: 0;">EMAIL THÔNG BÁO</h2>
	        </div>
	        <div style="clear:both"></div>
	        <div style="width:100%; height: 23px; float: left;">
	            <div style="float: left; line-height: 35px;">'.date('d/m/Y').'</div>
	            <ul style="float: right; margin: 0; padding: 0;">
	                <li style="list-style: none; float: left; margin-right: 20px; padding: 10px 0;"><a href="'.PG_URL_ABSOLUTE.'" target="_blank" style="color: #FE890A; text-decoration: none; line-height: 20px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_soha.jpg" style="float:left; border:none;" />Onecard.vn</a></li>
	                <li style="list-style: none; float: left; margin-right: 20px; padding: 10px 0;"><a href="'.PG_URL_ABSOLUTE.'info/contact.html" target="_blank" style="color: #FE890A; text-decoration: none; line-height: 20px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_contact.jpg" style="float:left; border:none;" />Liên hệ</a></li>
	                <li style="list-style: none; float: left; margin-right: 20px; padding: 10px 0;"><a href="http://www.facebook.com/OnecardVN" target="_blank" style="color: #FE890A; text-decoration: none; line-height: 20px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_face.jpg" style="float:left; border:none;" />Facebook</a></li>
	            </ul>
	        </div>
	        <div style="clear:both"></div>
	    </div>
	    <div style="clear:both"></div>
	    <div style="color: #000000;padding: 10px 0 0 0;">'.$message.'</div>
	    <div style="text-align: center; color: #FE890A; font-style: italic; font-size: 15px; width: 100%; float: left; padding-top: 10px; font-weight: bold; line-height: 25px;">Onecard - All in one</div>
	</div>
	<div style="width: 723px; padding: 10px 0; text-align: center; color: #333; font-weight: normal; font-size: 12px; font-family: Arial, Tahoma, Verdana;">
	    <ul style="margin: 0; padding: 0; margin-left: 40px;">
	        <li style="list-style: none; float: left; margin-right: 30px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_contact.jpg" style="float:left; border:none; padding-right:3px; margin-top:-3px;" />Điện thoại: 04.37476106</li>
	        <li style="list-style: none; float: left; margin-right: 30px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_mail.jpg" style="float:left; border:none; padding-right:3px; margin-top:-3px;" />Email: <a style="color: #FE890A; text-decoration: none;" href="mailto:lienhe@onecard.vn">lienhe@onecard.vn</a></li>
	        <li style="list-style: none; float: left; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_yahoo.jpg" style="float:left; border:none; padding-right:3px; margin-top:-3px;" /><a style="color: #FE890A; text-decoration: none;" href="ymsgr:sendim?hotro_onecard">YM: hotro_onecard</a></li>
	    </ul>
	</div>
	';

	return send_email($recipient, $sender, $subject, $message, $order_id);
} // END FUNCTION

function getSystemEmail($name, $aryReplace=null) {
	global $setting, $database;
	$sql = "SELECT * FROM system_emails WHERE system_email_name='".mysql_escape_string($name)."' LIMIT 1";
	$aryEmail = $database->db_fetch_assoc($database->db_query($sql));

	if ($aryEmail['system_email_body'] != '') {
		$aryEmail['system_email_body'] = str_replace(array_keys($aryReplace), $aryReplace, $aryEmail['system_email_body']);
	}
	return $aryEmail;
}

function setSubscribeBody($subscribe = array(), $flag = false) {
	if ($flag) {
		$content = '
			<div style="width: 723px; background-color: #FFFFFF; border: 1px solid #CDCDCD; position: relative; padding: 10px; font-family: Arial, Tahoma, Verdana; font-size: 12px; color: #999; display: inline-block; border-radius:5px; -webkit-border-radius:5px; -moz-border-radius:5px;">	
				<div style="width: 100%; height: 84px; border-bottom: 1px dotted #999999;">
					<a href="'.PG_URL_ABSOLUTE.'" style="width: 200px; height: 53px; display: block; float: left;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/logo.png" alt="Logo SohaPay" width="200" height="53" border="0" /></a>
					<div style="float: right; font-size: 12px; color: #999; text-align:right">
						<h2 style="color: #FE890A; font-size: 22px; margin: 0;">THƯ ĐIỆN TỬ</h2>
						<a href="'.PG_URL_ABSOLUTE.'subscribe.php?task=unsub&email=@subcribe_email&id=@subcribe_id" target="_blank" style="color: #999; text-decoration:none;">Huỷ nhận email khuyễn mãi từ sohapay.com</a>
					</div>
					<div style="clear:both"></div>
					<div style="width:100%; height: 23px; float: left;">
						<div style="float: left; line-height: 35px;">[product_date]</div>
						<ul style="float: right; margin: 0; padding: 0;">
							<li style="list-style: none; float: left; margin-right: 20px; padding: 10px 0;"><a href="'.PG_URL_ABSOLUTE.'" target="_blank" style="color: #FE890A; text-decoration: none; line-height: 20px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_soha.jpg" style="float:left; border:none;" />Sohapay.com</a></li>
							<li style="list-style: none; float: left; margin-right: 20px; padding: 10px 0;"><a href="'.PG_URL_ABSOLUTE.'info/contact.html" target="_blank" style="color: #FE890A; text-decoration: none; line-height: 20px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_contact.jpg" style="float:left; border:none;" />Liên hệ</a></li>
							<li style="list-style: none; float: left; margin-right: 20px; padding: 10px 0;"><a href="http://www.facebook.com/pages/SohaPay/301383356548312" target="_blank" style="color: #FE890A; text-decoration: none; line-height: 20px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_face.jpg" style="float:left; border:none;" />Facebook</a></li>
						</ul>
					</div>
				</div>
				<div style="font-size: 22px; line-height: 30px; font-weight: bold; color: #333; padding-top: 3px;">
				<a style="color:#333; text-decoration: none;" target="_blank" href="[product_link]">[product_title]</a>
				</div>
				<div style="clear:both"></div>
				<div style="width: 280px; float: left; display: block;">
					<div style="background: #ECF0F4; position: relative; color: #000; line-height: 20px; padding:5px 0; -moz-border-radius:8px; -webkit-border-radius:8px; border-radius:8px;">				
						<div style="padding: 10px; margin: 0;line-height:150%;">[product_description]</b></div>
						<p style="padding: 10px; margin: 0;" align="center">
							<div style="width: 140px; height: 47px; background: #01498B; text-align: center; color: #fff; font-weight: bold; cursor: pointer; position: relative; display: block; line-height: 47px; margin-left: 66px; -moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px;">						
								<a target="_blank" style="color: #fff; text-decoration: none;" href="[product_link]">Xem chi tiết</a>
							</div>
						</p>
					</div>
					<div style="border: 1px solid #CCCCCC; position: relative; margin-top: 10px; text-align: center; color: #CC3300; font-weight: bold; font-size: 16px; -moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px;">				
						<p style="margin: 5px 0;">Giảm <br /> 
						<b style="font-size: 30px;">[product_discount]%</b></p>
					</div>
				</div>
				<div style="width: 15px; float:left; margin-top:20px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/rowi.jpg" border="0" /></div>
				<div style="width: 408px; float: right; display: block; margin-right: 10px;">
					<div style="padding: 3px; text-align: center; border: 1px solid #A6C0D8;">
						<a href="[product_link]"><img src="[product_image]" width="398" height="266" alt="Ảnh sản phẩm" style="margin: 0 auto; border: none;" /></a>			</div>
					<div style="clear:both"></div>
					<div style="width: 100%; border-bottom: 1px dotted #ccc; float: left;">
						<div style="color: #CC3300; float: left;">Giá bán: <b style="font-size: 26px;">[product_price] đ</b></div>
						<div style="color: #01498B; float: right;">Giá gốc: <b style="font-size: 26px;">[product_cost] đ</b></div>
					</div>
				</div>
				<div style="text-align: center; color: #FE890A; font-style: italic; font-size: 15px; width: 100%; float: left; padding-top: 10px; font-weight: bold; line-height: 25px;">Thanh toán an toàn và dễ dàng với SohaPay</div>
			</div>
			<div style="width: 723px; padding: 10px 0; text-align: center; color: #333; font-weight: normal; font-size: 12px; font-family: Arial, Tahoma, Verdana;">
				<ul style="margin: 0; padding: 0; margin-left: 40px;">
					<li style="list-style: none; float: left; margin-right: 30px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_contact.jpg" style="float:left; border:none; padding-right:3px; margin-top:-3px;" />Điện thoại: 04.36321221 - máy lẻ: 123 hoặc 851</li>
					<li style="list-style: none; float: left; margin-right: 30px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_mail.jpg" style="float:left; border:none; padding-right:3px; margin-top:-3px;" />Email: <a style="color: #FE890A; text-decoration: none;" href="mailto:hotro@sohapay.com">hotro@sohapay.com</a></li>
					<li style="list-style: none; float: left; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_yahoo.jpg" style="float:left; border:none; padding-right:3px; margin-top:-3px;" /><a style="color: #FE890A; text-decoration: none;" href="ymsgr:sendim?hotro_sohapay">YM: hotro_sohapay</a></li>
				</ul>
			</div>
		';
		return $content;
	}
	$content = '
		<div style="width: 723px; background-color: #FFFFFF; border: 1px solid #CDCDCD; position: relative; padding: 10px; font-family: Arial, Tahoma, Verdana; font-size: 12px; color: #999; display: inline-block; border-radius:5px; -webkit-border-radius:5px; -moz-border-radius:5px;">	
			<div style="width: 100%; height: 84px; border-bottom: 1px dotted #999999;">
				<a href="'.PG_URL_ABSOLUTE.'" style="width: 200px; height: 53px; display: block; float: left;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/logo.png" alt="Logo SohaPay" width="200" height="53" border="0" /></a>
				<div style="float: right; font-size: 12px; color: #999; text-align:right">
					<h2 style="color: #FE890A; font-size: 22px; margin: 0;">THƯ ĐIỆN TỬ</h2>
					<a href="'.PG_URL_ABSOLUTE.'subscribe.php?task=unsub&email=@subcribe_email&id=@subcribe_id" target="_blank" style="color: #999; text-decoration:none;">Huỷ nhận email khuyễn mãi từ sohapay.com</a>
				</div>
				<div style="clear:both"></div>
				<div style="width:100%; height: 23px; float: left;">
					<div style="float: left; line-height: 35px;">' . $subscribe['time'] .'</div>
					<ul style="float: right; margin: 0; padding: 0;">
						<li style="list-style: none; float: left; margin-right: 20px; padding: 10px 0;"><a href="'.PG_URL_ABSOLUTE.'" target="_blank" style="color: #FE890A; text-decoration: none; line-height: 20px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_soha.jpg" style="float:left; border:none;" />Sohapay.com</a></li>
						<li style="list-style: none; float: left; margin-right: 20px; padding: 10px 0;"><a href="'.PG_URL_ABSOLUTE.'info/contact.html" target="_blank" style="color: #FE890A; text-decoration: none; line-height: 20px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_contact.jpg" style="float:left; border:none;" />Liên hệ</a></li>
						<li style="list-style: none; float: left; margin-right: 20px; padding: 10px 0;"><a href="http://www.facebook.com/pages/SohaPay/301383356548312" target="_blank" style="color: #FE890A; text-decoration: none; line-height: 20px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_face.jpg" style="float:left; border:none;" />Facebook</a></li>
					</ul>
				</div>
			</div>
			<div style="font-size: 22px; line-height: 30px; font-weight: bold; color: #333; padding-top: 3px;">
			<a style="color:#333; text-decoration: none;" target="_blank" href="' . $subscribe['link'] . '">' . $subscribe['title']. '</a>
			</div>
			<div style="clear:both"></div>
			<div style="width: 280px; float: left; display: block;">
				<div style="background: #ECF0F4; position: relative; color: #000; line-height: 20px; padding:5px 0; -moz-border-radius:8px; -webkit-border-radius:8px; border-radius:8px;">				
					<div style="padding: 10px; margin: 0;line-height:150%;">' . $subscribe['description'] . '</div>
					<p style="padding: 10px; margin: 0;" align="center">
						<div style="width: 140px; height: 47px; background: #01498B; text-align: center; color: #fff; font-weight: bold; cursor: pointer; position: relative; display: block; line-height: 47px; margin-left: 66px; -moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px;">						
							<a target="_blank" style="color: #fff; text-decoration: none;" href="'.PG_URL_ABSOLUTE.'info/san-pham-cong-nghe/42-dien-thoai-thoi-trang-qmobile-f363-2sim-2song.html">Xem chi tiết</a>
						</div>
					</p>
				</div>
				<div style="border: 1px solid #CCCCCC; position: relative; margin-top: 10px; text-align: center; color: #CC3300; font-weight: bold; font-size: 16px; -moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px;">				
					<p style="margin: 5px 0;">Giảm <br /> 
					<b style="font-size: 30px;">' . $subscribe['discount'] .'%</b></p>
				</div>
			</div>
			<div style="width: 15px; float:left; margin-top:20px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/rowi.jpg" border="0" /></div>
			<div style="width: 408px; float: right; display: block; margin-right: 10px;">
				<div style="padding: 3px; text-align: center; border: 1px solid #A6C0D8;">
					<a href="' . $subscribe['link']  . '"><img src="' . $subscribe['image'] . '" width="398" height="266" alt="Ảnh sản phẩm" style="margin: 0 auto; border: none;" /></a>			</div>
				<div style="clear:both"></div>
				<div style="width: 100%; border-bottom: 1px dotted #ccc; float: left;">
					<div style="color: #CC3300; float: left;">Giá bán: <b style="font-size: 26px;">' .  $subscribe['price'] . ' đ</b></div>
					<div style="color: #01498B; float: right;">Giá gốc: <b style="font-size: 26px;">' .  $subscribe['cost'] . ' đ</b></div>
				</div>
			</div>
			<div style="text-align: center; color: #FE890A; font-style: italic; font-size: 15px; width: 100%; float: left; padding-top: 10px; font-weight: bold; line-height: 25px;">Thanh toán an toàn và dễ dàng với SohaPay</div>
		</div>
		<div style="width: 723px; padding: 10px 0; text-align: center; color: #333; font-weight: normal; font-size: 12px; font-family: Arial, Tahoma, Verdana;">
			<ul style="margin: 0; padding: 0; margin-left: 40px;">
				<li style="list-style: none; float: left; margin-right: 30px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_contact.jpg" style="float:left; border:none; padding-right:3px; margin-top:-3px;" />Điện thoại: 04.36321221 - máy lẻ: 123 hoặc 851</li>
				<li style="list-style: none; float: left; margin-right: 30px; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_mail.jpg" style="float:left; border:none; padding-right:3px; margin-top:-3px;" />Email: <a style="color: #FE890A; text-decoration: none;" href="mailto:hotro@sohapay.com">hotro@sohapay.com</a></li>
				<li style="list-style: none; float: left; padding-left: 5px;"><img src="'.PG_URL_ABSOLUTE.'templates/images/email/li_yahoo.jpg" style="float:left; border:none; padding-right:3px; margin-top:-3px;" /><a style="color: #FE890A; text-decoration: none;" href="ymsgr:sendim?hotro_sohapay">YM: hotro_sohapay</a></li>
			</ul>
		</div>
	';
	return $content;
}
?>